**Inventory Analytical System Tree v2.0 (Fully Developed)**

*Version: 2.0 | Date: Current | Status: Actionable Blueprint for Implementation*
*Prerequisites: XAMPP (Apache/MySQL/PHP 8+), VSCode, Browser. DB: invanalytics (from invanalytics.sql).*

## 1. Tech Stack Tree
```
InvAnalytics Stack
├── Backend: PHP 8+ / MySQL 10.4+
├── Frontend: HTML/CSS/JS + Bootstrap 5 (or React if scaling)
├── DB: MariaDB (invanalytics.sql schema)
├── API: RESTful PHP endpoints
├── Charts: Chart.js
└── Deployment: XAMPP htdocs/invanalytic
```

## 2. Data Layer (Database Schema)
*Current: 3 tables from invanalytics.sql. Enhancements below.*

**ER Diagram (ASCII):**
```
orders --(Order_No)--> grv --(ItemCode)--> inventory_transaction
     --(ItemCode)--> items_master <--(SupplierID)--> suppliers
```

**Missing Tables (Add to invanalytics.sql):**
```sql
CREATE TABLE items_master (
  ItemCode VARCHAR(50) PRIMARY KEY,
  ItemName VARCHAR(255),
  Category VARCHAR(100),
  UnitOfMeasure VARCHAR(20),
  ReorderLevel DECIMAL(18,4) DEFAULT 0
);

CREATE TABLE suppliers (
  SupplierID VARCHAR(50) PRIMARY KEY,
  SupplierName VARCHAR(255),
  LeadTime_Days INT DEFAULT 0,
  Rating DECIMAL(3,2) DEFAULT 0.00
);

-- Sample Data
INSERT INTO items_master VALUES ('CHEM001', 'Acid Solution', 'Chemical', 'L', 100);
INSERT INTO suppliers VALUES ('SUP001', 'ChemCorp', 7, 4.5);
```

**KPIs Tree:**
```
Data KPIs
├── Stock On Hand (SOH) = SUM(quantity_in - quantity_out)
├── Weighted Avg Cost
├── Total Value = SOH * AvgCost
└── Turns = Total Issues / Avg Inventory
```

## 3. Integration Layer (PHP API)
*File Structure: api/ (create folder)*
```
api/
├── config.php (DB connect)
├── procurement.php (GET /procurement?filter=date)
├── inventory.php (GET /inventory)
└── utils.php (shared functions)
```

**Example: api/procurement.php**
```php
<?php
include 'config.php';
$filters = $_GET['filter'] ?? '';
$sql = "SELECT o.*, g.Quantity_Received, (g.Quantity_Received / o.Quantity * 100) AS fulfillment 
        FROM orders o LEFT JOIN grv g ON o.OrderID = g.GRV_ID WHERE $filters";
$result = $conn->query($sql);
echo json_encode($result->fetch_all());
?>
```

## 4. Logic Layer (Business Intelligence)
**Modules as PHP Functions (in lib/logic.php):**

**Fulfillment Service:**
```php
function calcFulfillment($ordered, $received) {
    return $received > $ordered ? 100 : ($received / $ordered * 100);
}
```

**Stock Valuation:**
```php
function stockValue($soh, $costs) { // costs = array from history
    $avgCost = array_sum($costs) / count($costs);
    return $soh * $avgCost;
}
```

**Alert System:**
```php
function checkReorder($itemCode, $pdo) {
    $stmt = $pdo->query("SELECT SOH, ReorderLevel FROM v_inventory WHERE ItemCode='$itemCode'");
    return $stmt->fetch()['SOH'] < $stmt->fetch()['ReorderLevel'];
}
```

**Cost Analytics Tree:**
```
Cost Tree
├── Procurement Cost: SUM(UnitPrice * Quantity)
├── Inventory Value
├── Variance: Proc Cost - Invoiced
└── Forecast: Trendline (3-month moving avg)
```

## 5. Presentation Layer (Frontend Tree)
*File Structure: public/*
```
public/
├── index.html (Dashboard)
├── css/style.css
├── js/app.js (fetch API, render charts)
├── dashboard.html
├── procurement.html
└── charts/ (Chart.js)
```

**UI Wireframe (ASCII - Dashboard):**
```
+-------------------------------+
| Header: User | Notifications |
+---------------+---------------+
| KPI Cards     | Chart Grid    |
| - Value: $xx  | LeadTime -->  |
| - POs: 15     | Fulfillment O |
| - Low Stock: 3| Stock Value []|
+-------------------------------+
| Data Table (Sortable)         |
+-------------------------------+
```

**Sample index.html Snippet:**
```html
<!DOCTYPE html>
<html>
<head><script src="https://cdn.jsdelivr.net/npm/chart.js"></script></head>
<body>
  <canvas id="fulfillmentChart"></canvas>
  <script>
    fetch('/api/procurement.php').then(r=>r.json()).then(data=> {
      new Chart(document.getElementById('fulfillmentChart'), {type:'doughnut', data});
    });
  </script>
</body>
</html>
```

**UI Components Tree:**
```
Views
├── Dashboard (KPIs + Charts)
├── Procurement (Grid + Filters + Modals)
├── Inventory (Cards by Warehouse)
└── Reports (Export CSV/PDF)
```

## 6. Operational Workflows (Flowcharts)
**Procurement Flow (ASCII):**
```
PO Created --> Approved? --> No: Pending
             |
             Yes --> Receive GRV --> Post to Inventory --> Closed if 100%
```

**Error Handling:** Rollback on DB errors; UI alerts for low stock.

## Implementation Roadmap
1. Update DB: Run enhanced SQL.
2. Create API folder/files.
3. Build public/ frontend.
4. Test: `http://localhost/invanalytic/public/index.html`
5. Deploy: XAMPP start.

*End of System Tree. Track in TODO.md.*

